<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

class Model_Nilai_Siswa extends MY_Model {
	
	public function __construct() {

		parent::__construct();

		$this->table_name = 'aka_nilai_detail';

		$this->primary_key = 'aka_nilai_detail.nilai_detail_id';

	}
	function get_nilai_siswa($departemen_id,$tahun_ajaran_id,$semester_id,$rapor_id,$pelajaran_id,$kelas_id,$pembagian_kelas_id,$aktif=null){
	
		$this->load->database();
		$filter = "";
			if($aktif != null)
				$filter = "WHERE {$this->table_name}.aktif='$aktif'";
				
			  $sql = "SELECT aka_nilai_detail.*,aka_departemen.nama,aka_tahun_ajaran.tahun_ajaran,aka_semester.semester,aka_kategori_rapor.nama_rapor,aka_pelajaran.nama,aka_tingkat.tingkat,aka_kelas.kelas FROM aka_nilai_detail JOIN aka_departemen ON aka_nilai_detail.departemen_id=aka_departemen.departemen_id JOIN aka_tahun_ajaran ON aka_nilai_detail.tahun_ajaran_id=aka_tahun_ajaran.tahun_ajaran_id JOIN aka_semester ON aka_nilai_detail.semester_id=aka_semester.semester_id JOIN aka_kategori_rapor ON aka_nilai_detail.kategori_rapor_id=aka_kategori_rapor.kategori_rapor_id JOIN aka_pelajaran ON aka_nilai_detail.pelajaran_id=aka_pelajaran.pelajaran_id JOIN aka_tingkat ON aka_nilai_detail.tingkat_id=aka_tingkat.tingkat_id JOIN aka_kelas ON aka_nilai_detail.kelas_id=aka_kelas.kelas_id WHERE aka_nilai_detail.departemen_id='$departemen_id' AND aka_nilai_detail.tahun_ajaran_id='$tahun_ajaran_id' AND aka_nilai_detail.semester_id='$semester_id' AND aka_nilai_detail.kategori_rapor_id='$rapor_id' AND aka_nilai_detail.pelajaran_id='$pelajaran_id' AND aka_nilai_detail.tingkat_id='$kelas_id' AND aka_nilai_detail.kelas_id='$pembagian_kelas_id' $filter ORDER BY aka_nilai_detail.nilai_detail_id";
			  
			  
			  /*$sql = "select aka_nilai_detail.*,aka_tingkat.tingkat, aka_pelajaran.nama as pelajaran, aka_kelas.kelas from aka_nilai_detail JOIN aka_nilai_semester ON aka_nilai_detail.nilai_semester_id=aka_nilai_semester.nilai_semester_id JOIN aka_pelajaran ON aka_nilai_detail.pelajaran_id=aka_pelajaran.pelajaran_id JOIN aka_kelas ON aka_nilai_detail.kelas_id=aka_kelas.kelas_id JOIN aka_departemen ON aka_nilai_detail.departemen_id=aka_departemen.departemen_id JOIN aka_tahun_ajaran ON aka_nilai_detail.tahun_ajaran_id=aka_tahun_ajaran.tahun_ajaran_id JOIN aka_semester ON aka_nilai_detail.semester_id=aka_semester.semester_id JOIN aka_kategori_rapor ON aka_nilai_detail.kategori_rapor_id=aka_kategori_rapor.kategori_rapor_id JOIN aka_tingkat ON aka_nilai_detail.tingkat_id=aka_tingkat.tingkat_id JOIN mst_pegawai ON aka_nilai_detail.guru_id=mst_pegawai.pegawai_id WHERE aka_nilai_detail.departemen_id='$departemen_id' AND aka_nilai_detail.tahun_ajaran_id='$tahun_ajaran_id' AND aka_nilai_detail.semester_id='$semester_id' AND aka_nilai_detail.kategori_rapor_id='$rapor_id' AND aka_nilai_detail.pelajaran_id='$pelajaran_id' AND aka_nilai_detail.tingkat_id='$kelas_id' AND aka_nilai_detail.kelas_id='$pembagian_kelas_id' $filter ORDER BY aka_nilai_detail.nilai_detail_id"; */
				
		$query = $this->db->query($sql);
		$this->db->close();
		return $query;
	}
	function get_nilai_semester(){
		$this->load->database();		
		$sql = "SELECT * FROM aka_nilai_detail ORDER BY aka_nilai_detail.nilai_detail_id";
		$query = $this->db->query($sql);
		$this->db->close();
		return $query;
	}
	
	function get_by($id){
		$this->load->database();		
		$sql = "SELECT * FROM {$this->table_name} WHERE {$this->primary_key}='$id';";
		$query = $this->db->query($sql);
		$this->db->close();
		return $query;
	}
	
	function add($data){
		$this->load->database();
			$this->db->trans_start();
				$this->db->insert($this->table_name,$data);
			$this->db->trans_complete();
			
			if ($this->db->trans_status() === FALSE)
			{
				$this->session->set_flashdata('error','Data gagal disimpan.');
			}else{
				$this->session->set_flashdata('success','Data berhasil disimpan.');
			}
		$this->db->close();
	}
	
	function edit($id,$data){
		$this->load->database();
			$this->db->trans_start();
				$this->db->where($this->primary_key,$id);
				$this->db->update($this->table_name,$data);
			$this->db->trans_complete();
			
			if ($this->db->trans_status() === FALSE)
			{
				$this->session->set_flashdata('error','Data gagal disimpan.');
			}else{
				$this->session->set_flashdata('success','Data berhasil disimpan.');
			}
		$this->db->close();
	}
	
	function delete($id){
		$this->load->database();
			$this->db->trans_start();
				$sql = "DELETE FROM {$this->table_name} WHERE {$this->primary_key}='$id'";
				$this->db->query($sql);
			$this->db->trans_complete();
			
			if ($this->db->trans_status() === FALSE)
			{
				$this->session->set_flashdata('error','Data gagal dihapus.');
			}else{
				$this->session->set_flashdata('success','Data berhasil dihapus.');
			}
		$this->db->close();
	}
	
}

?>